package com.jinzhi.jzweb.controller;

import static com.jinzhi.api.util.TokenTag.TOKEN_TAG;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.jzapi.training.pojo.ByTraResourcesListVo;
import com.jinzhi.jzapi.training.pojo.CoursesListVo;
import com.jinzhi.jzweb.domain.ChapterDO;
import com.jinzhi.jzweb.domain.ChapterItemBankDO;
import com.jinzhi.jzweb.domain.CoursesDO;
import com.jinzhi.jzweb.domain.OrderDO;
import com.jinzhi.jzweb.domain.TrainingResourcesDO;
import com.jinzhi.jzweb.service.CertificateProfileService;
import com.jinzhi.jzweb.service.CertificatesService;
import com.jinzhi.jzweb.service.ChapterItemBankService;
import com.jinzhi.jzweb.service.ChapterService;
import com.jinzhi.jzweb.service.CoursesService;
import com.jinzhi.jzweb.service.TrainingResourcesService;
import com.jinzhi.sys.domain.UserDO;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

@Controller
@RequestMapping("/jinzhi/openCourse")
public class OpenCourseController extends BaseController{
	
	@Autowired
    private CoursesService coursesService;
	@Autowired
    private CertificateProfileService certificateProfileService;
	@Autowired
    private ChapterService chapterService;
	@Autowired
    private ChapterItemBankService chapterItemBankService;
	@Autowired
    private TrainingResourcesService trainingResourcesService;
	
    @GetMapping("/course-categories")
    @ResponseBody
    public ApiResult<?> selProfileFill() {
        List<Map<Object, Object>> maps = certificateProfileService.selByAll();
        return ApiResult.success(maps);
    }
	
    @GetMapping("/course")
    @ResponseBody
    public ApiResult<?> selCourseList(String cid, String title) {
        List<CoursesDO> courseList = coursesService.findByKv("profileId",cid,"name",title);
        List<Map> list = new ArrayList<Map>();
        for(CoursesDO c : courseList) {
        	Map m = new HashMap();
        	m.put("id",c.getId());
        	m.put("open_id",c.getId());
        	m.put("title",c.getName());
        	m.put("image",c.getThumbnail());
        	m.put("description",c.getIntroduce());
        	m.put("price", c.getSpecialPrice());
        	m.put("cost_price", c.getGuidancePrice());
        	m.put("total_video", 0);
        	m.put("total_time", 0);
        	m.put("total_period", 0);
        	m.put("click", 0);
        	m.put("sort", 0);
        	m.put("hide", 0);
        	m.put("create_time", 0);
        	m.put("update_time", 0);
        	Map hideName = new HashMap();
        	hideName.put("name","上架");
        	m.put("hideName", hideName);
        	list.add(m);
        }
        return ApiResult.success(list);
    }
    
    @GetMapping("/course/{id}")
    @ResponseBody
    public ApiResult<?> selCourseList(@PathVariable(value="id",required=false)String id) {
        CoursesDO c = coursesService.findOneByKv("id",id);
        Map m = new HashMap();
        if(c!=null) {
        	m.put("id",c.getId());
        	m.put("open_id",c.getId());
        	m.put("title",c.getName());
        	m.put("image",c.getThumbnail());
        	m.put("description",c.getIntroduce());
        	m.put("price", c.getSpecialPrice());
        	m.put("cost_price", c.getGuidancePrice());
        	m.put("total_video", 0);
        	m.put("total_time", 0);
        	m.put("total_period", 0);
        	m.put("click", 0);
        	m.put("sort", 0);
        	m.put("hide", 0);
        	m.put("create_time", 0);
        	m.put("update_time", 0);
        	Map hideName = new HashMap();
        	hideName.put("name","上架");
        	m.put("hideName", hideName);
        	return ApiResult.success(m);
        }else {
        	return ApiResult.build(ApiEnumError.formatFail.getCode(), "参数为空或格式错误");
        }
        
    }
    
    @GetMapping("/course-chapters")
    @ResponseBody
    public ApiResult<?> selChapterList(String course_id,String title) {
        if (StringUtils.isNotBlank(course_id)) {
            List<Map> mapList = new ArrayList<>();
            List<ChapterDO> chapterDOS = chapterService.findByKv("courses_id", Long.parseLong(course_id),"name",title);
            if (chapterDOS != null && chapterDOS.size() > 0) {
                for (ChapterDO chapterDO : chapterDOS) {
                    Map map1 = new HashMap();
                    map1.put("id", chapterDO.getId());
                    map1.put("course_id", course_id);
                    List<ChapterItemBankDO> chapterItemBankDOS = chapterItemBankService.findByKv("chapter_id", chapterDO.getId(), "type", 0);
                    if (chapterDO.getName() != null) {
                        map1.put("title", chapterDO.getName());
                    } else {
                        map1.put("title", "");
                    }
                    map1.put("create_time",chapterDO);
                    map1.put("description",chapterDO.getIntroduce());
                    map1.put("sort",0);
                    map1.put("hide",0);
                    map1.put("create_time","");
                    map1.put("update_time","");
                    mapList.add(map1);
                }
            }
            return ApiResult.success(mapList);
        } else {
            return ApiResult.build(ApiEnumError.formatFail.getCode(), "参数为空或格式错误");
        }
    }
    
    @GetMapping("/course-chapters/{id}")
    @ResponseBody
    public ApiResult<?> selChapterList(@PathVariable(value="id",required=false)String id) {
        if (StringUtils.isNotBlank(id)) {
        	Map map1 = new HashMap();
            ChapterDO chapterDO = chapterService.findOneByKv("id",id);
            if (chapterDO != null) {
                    map1.put("id", chapterDO.getId());
                    map1.put("course_id", chapterDO.getCoursesId());
                    List<ChapterItemBankDO> chapterItemBankDOS = chapterItemBankService.findByKv("chapter_id", chapterDO.getId(), "type", 0);
                    if (chapterDO.getName() != null) {
                        map1.put("title", chapterDO.getName());
                    } else {
                        map1.put("title", "");
                    }
                    map1.put("create_time",chapterDO);
                    map1.put("description",chapterDO.getIntroduce());
                    map1.put("sort",0);
                    map1.put("hide",0);
                    map1.put("create_time","");
                    map1.put("update_time","");
                    return ApiResult.success(map1);
            }else {
            	return ApiResult.build(ApiEnumError.formatFail.getCode(), "参数为空或格式错误");
            }
            
        } else {
            return ApiResult.build(ApiEnumError.formatFail.getCode(), "参数为空或格式错误");
        }
    }
    
	
    @GetMapping("/course-videos")
    @ResponseBody
    public ApiResult<?> selChapterDetails(String course_id,String chapter_id,String title) {
        List<TrainingResourcesDO> trainningResourcesList = trainingResourcesService.findByKv("courses_id",course_id,"chapter_id",chapter_id,"name",title);
        List<Map> list = new ArrayList<Map>();
        for(TrainingResourcesDO traRes : trainningResourcesList) {
        	System.out.println(traRes.getType());
        	if(traRes.getType()==1) {
	        	Map map = new HashMap();
	        	map.put("id",traRes.getId());
	        	map.put("open_id",traRes.getId());
	        	map.put("course_id",traRes.getCoursesId());
	        	map.put("chapter_id",traRes.getChapterId());
	        	map.put("image","");
	        	map.put("title",traRes.getName());
	        	map.put("description","");
	        	map.put("url",traRes.getFileRoute());
	        	map.put("time",traRes.getVideoDuration());
	        	map.put("size","");
	        	map.put("sort",0);
	        	map.put("hide",0);
	        	map.put("create_time",traRes.getCreaterDate());
	        	map.put("update_time",traRes.getUpdateDate());
	        	map.put("id",traRes.getId());
	        	map.put("id",traRes.getId());
	        	list.add(map);
        	}
        }
        return ApiResult.success(list);
    }
    
    @GetMapping("/course-videos/{id}")
    @ResponseBody
    public ApiResult<?> selChapterDetails(@PathVariable(value="id",required=false)String id) {
        TrainingResourcesDO traRes = trainingResourcesService.findOneByKv("id",id);
        Map map = new HashMap();
        if(traRes!=null) {
        	if(traRes.getType()==1) {
	        	map.put("id",traRes.getId());
	        	map.put("open_id",traRes.getId());
	        	map.put("course_id",traRes.getCoursesId());
	        	map.put("chapter_id",traRes.getChapterId());
	        	map.put("image","");
	        	map.put("title",traRes.getName());
	        	map.put("description","");
	        	map.put("url",traRes.getFileRoute());
	        	map.put("time",traRes.getVideoDuration());
	        	map.put("size","");
	        	map.put("sort",0);
	        	map.put("hide",0);
	        	map.put("create_time",traRes.getCreaterDate());
	        	map.put("update_time",traRes.getUpdateDate());
	        	map.put("id",traRes.getId());
	        	map.put("id",traRes.getId());
        	}else {
        		return ApiResult.build(ApiEnumError.formatFail.getCode(), "参数为空或格式错误");
        	}
        	return ApiResult.success(map);
        }else {
        	return ApiResult.build(ApiEnumError.formatFail.getCode(), "参数为空或格式错误");
        }
    }
    
}
